package org.quickserver.net.GB268753QN;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.quickserver.net.model.GB268753QN;
import org.quickserver.net.model.GB26875SetUp;
import org.quickserver.net.model.GBServerData;
import org.quickserver.net.server.ClientHandler;
import org.quickserver.util.data.dataProcessing;




public class analysisMain
{
  public static GB268753QN init(ClientHandler handler, String[] strs) throws IOException {
    String logStrAll = dataProcessing.strsToStr(strs);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    GBServerData ec = (GBServerData)handler.getClientData();
    GB268753QN GBQN = new GB268753QN();
    int strsLength = strs.length;
    int dataLength = dataProcessing.sumHexStringToInt10(strs[24], strs[25]);
    String crcData = strs[strsLength - 3];
    String crcCalc = dataProcessing.crc_8(dataProcessing.strsToStrs(strs, 2, strsLength - 4));
    String headTail = String.valueOf(strs[0]) + strs[1] + strs[strsLength - 2] + strs[strsLength - 1];
    String serialNumber = String.valueOf(strs[2]) + strs[3];
    String[] mainMata = dataProcessing.strsToStrs(strs, 27, strsLength - 4);
    //String[] otherData = dataProcessing.strsToStrs(strs, 2, 23);

    String  s1=dataProcessing.strsToStr(dataProcessing.strsToStrs(strs, 2, 5));    // 流水号  协议版本号
    String  s2=dataProcessing.strsToStr(dataProcessing.getDateTime16(new Date()));   //时间
    String  s3=dataProcessing.strsToStr(dataProcessing.strsToStrs(strs, 12,17));   //源地址
    String  s4=dataProcessing.strsToStr(dataProcessing.strsToStrs(strs, 18, 23));   //  目标地址

    String otherData=s1+s2+s4+s3;


    String otherTime = String.valueOf(strs[11]) + strs[10] + strs[9] + strs[8] + strs[7] + strs[6];
    int otherCommand = dataProcessing.hexStringToInt10(strs[26]);
    int typeMark = dataProcessing.hexStringToInt10(strs[27]);
    String[] yesHm = { "40", "00", "03", "06", "23" };
    String yesCrc8Str = otherData + yesHm[1] + yesHm[1] + yesHm[2];
    String yesCrc8 = dataProcessing.crc_8(dataProcessing.bytesToHexStrings(dataProcessing.strToBytes(yesCrc8Str)));
    String yeshm = String.valueOf(yesHm[0]) + yesHm[0] + otherData + yesHm[1] + yesHm[1] + yesHm[2] +
      yesCrc8 + yesHm[4] + yesHm[4];
    String noCrc8Str = otherData + yesHm[1] + yesHm[1] + yesHm[3];
    String noCrc8 = dataProcessing.crc_8(dataProcessing.bytesToHexStrings(dataProcessing.strToBytes(noCrc8Str)));
    String nohm = String.valueOf(yesHm[0]) + yesHm[0] + otherData + yesHm[1] + yesHm[1] + yesHm[3] +
      noCrc8 + yesHm[4] + yesHm[4];
    
    byte[] hm = new byte[0];
    System.out.println(otherCommand + "--" + typeMark + "-----" + logStrAll);
    if (otherCommand == 2 && (
      !ec.getUniqueCode().equals("0") || typeMark == 26 || typeMark == 105 || typeMark == 107) && 
      "40402323".equals(headTail) && crcCalc.equals(crcData)) {
      try {
        Object o;
        
        if (otherCommand == 2 && typeMark == 122) {
          Class<?> clazz = 
            Class.forName("org.quickserver.net.GB268753QN.control.QNA" + otherCommand + "B" + typeMark);
          Method method = clazz.getMethod("QNA" + otherCommand + "B" + typeMark + "OBJ", new Class[] { ClientHandler.class, 
                String[].class, int.class });
          o = method.invoke(clazz.newInstance(), new Object[] { handler, mainMata, Integer.valueOf(dataLength) });
        } else {
          Class<?> clazz = 
            Class.forName("org.quickserver.net.GB268753QN.control.QNA" + otherCommand + "B" + typeMark);
          Method method = clazz.getMethod("QNA" + otherCommand + "B" + typeMark + "OBJ", new Class[] { ClientHandler.class, 
                String[].class });
          o = method.invoke(clazz.newInstance(), new Object[] { handler, mainMata });
        } 
        
        if (o == null) {
          GBQN = null;
          hm = dataProcessing.strToBytes(yeshm);
        }
        else {
          GBQN = (GB268753QN)o;
          GBQN.setHex(logStrAll);
          hm = dataProcessing.strToBytes(yeshm);
        }
      
      } catch (IllegalAccessException e) {

        
        GBQN = null;
        hm = dataProcessing.strToBytes(nohm);
        handler.sendClientBinary(hm);
        return GBQN;
      } catch (IllegalArgumentException e) {

        
        GBQN = null;
        hm = dataProcessing.strToBytes(nohm);
        handler.sendClientBinary(hm);
        return GBQN;
      } catch (InvocationTargetException e) {

        
        GBQN = null;
        hm = dataProcessing.strToBytes(nohm);
        handler.sendClientBinary(hm);
        return GBQN;
      } catch (InstantiationException e) {

        
        GBQN = null;
        hm = dataProcessing.strToBytes(nohm);
        handler.sendClientBinary(hm);
        return GBQN;
      } catch (NoSuchMethodException e1) {

        
        GBQN = null;
        hm = dataProcessing.strToBytes(nohm);
        handler.sendClientBinary(hm);
        return GBQN;
      } catch (SecurityException e1) {

        
        GBQN = null;
        hm = dataProcessing.strToBytes(nohm);
        handler.sendClientBinary(hm);
        return GBQN;
      } catch (ClassNotFoundException e2) {
        handler.sendSystemMsg("system Class org.quickserver.net.GB268753QN.control.QNA" + otherCommand + 
            "B" + typeMark + "******ClassNotFoundException!!!");
        
        String logStr = "";
        for (int s = 0; s < strs.length; s++) {
          logStr = String.valueOf(logStr) + strs[s];
        }
        GBQN = null;
        hm = dataProcessing.strToBytes(yeshm);
        handler.sendClientBinary(hm);
        return GBQN;
      } 
    } else if (otherCommand == 3 && "40402323".equals(headTail) && crcCalc.equals(crcData)) {
      List<GB26875SetUp> list = ec.getSerialNumberList();
      for (int i = 0; i < list.size(); i++) {
        GB26875SetUp gs = list.get(i);
        if (gs.getSerialNumber().equals(serialNumber)) {
          ((GB26875SetUp)list.get(i)).setTrue(true);
          ((GB26875SetUp)list.get(i)).setOk(true);
          break;
        } 
      } 
      GBQN = null;
      hm = dataProcessing.strToBytes(yeshm);
    } else if (otherCommand == 6 && "40402323".equals(headTail) && crcCalc.equals(crcData)) {
      List<GB26875SetUp> list = ec.getSerialNumberList();
      String hmStr = "";
      for (int i = 0; i < list.size(); i++) {
        GB26875SetUp gs = list.get(i);
        if (gs.getSerialNumber().equals(serialNumber)) {
          ((GB26875SetUp)list.get(i)).setTrue(false);
          ((GB26875SetUp)list.get(i)).setOk(true);
          hmStr = gs.getHex();
          
          break;
        } 
      } 
      
      GBQN = null;
      hm = dataProcessing.strToBytes(yeshm);
    } else if (otherCommand == 5 && "40402323".equals(headTail) && crcCalc.equals(crcData)) {
      try {
        Class<?> clazz = 
          Class.forName("org.quickserver.net.GB268753QN.control.QNA" + otherCommand + "B" + typeMark);
        Method method = clazz.getMethod("QNA" + otherCommand + "B" + typeMark + "OBJ", new Class[] { ClientHandler.class, 
              String[].class, String.class });
        method.invoke(clazz.newInstance(), new Object[] { handler, mainMata, serialNumber });
        hm = dataProcessing.strToBytes(yeshm);
      } catch (IllegalAccessException e) {

        
        hm = dataProcessing.strToBytes(yeshm);
      } catch (IllegalArgumentException e) {

        
        hm = dataProcessing.strToBytes(yeshm);
      } catch (InvocationTargetException e) {

        
        hm = dataProcessing.strToBytes(yeshm);
      } catch (InstantiationException e) {

        
        hm = dataProcessing.strToBytes(yeshm);
      } catch (NoSuchMethodException e1) {

        
        hm = dataProcessing.strToBytes(yeshm);
      } catch (SecurityException e1) {

        
        hm = dataProcessing.strToBytes(yeshm);
      } catch (ClassNotFoundException e2) {
        handler.sendSystemMsg("system Class org.quickserver.net.GB268753QN.control.QNA" + otherCommand +
            "B" + typeMark + "******ClassNotFoundException!!!");
        String logStr = "";
        for (int s = 0; s < strs.length; s++) {
          logStr = String.valueOf(logStr) + strs[s];
        }
        hm = dataProcessing.strToBytes(yeshm);
      } 
    } else {
      hm = dataProcessing.strToBytes(nohm);
    } 
    handler.sendClientBinary(hm);
    return GBQN;
  }
}
